package main.test.SparkAPI.Baics.collectapi

object testMAP {
  def testMaps(): Unit ={
    /**
     * The Map class documentation describes a Map as an iterable sequence that consists of pairs of keys and values. A simple Map looks like this
     */
    val states = Map(
      "AK" -> "Alaska",
      "IL" -> "Illinois",
      "KY" -> "Kentucky"
    )

    // create mutable Map
    import scala.collection.mutable.Map
    val sts = collection.mutable.Map("AK" -> "Alaska")
    // Adding elements to a Map
    // with +=
    sts += ("AL" -> "Alabama")
    // += add multiple elements
    sts += ("AR" -> "Arkansas", "AZ" -> "Arizona")
    // ++= ADD from another map
    sts ++= Map("CA" -> "California", "CO" -> "Colorado")
    println(sts.mkString("Map( ",", \n"," )"))


    // Removing elements from a Map
    // using -= and --=
    sts -= "AR"
    sts -= ("AL", "AZ")
    sts --= List("AL", "AZ")

    println(sts.mkString("Map( ",", \n"," )"))

    // You update Map elements by reassigning their key to a new value:
    sts("AK") = "aLASKA, a REALLY BIG STATE"
    println(sts.mkString("Map( ",", \n"," )"))

    // traversing a map
    val ratings = Map(
      "Lady in the Water"-> 3.0,
      "Snakes on a Plane"-> 4.0,
      "You, Me and Dupree"-> 3.5
    )
    for((k,v) <- ratings) println(s"key: $k, value: $v")

    // or using match and foreach
    ratings.foreach {
      case (movie, rating) => println(s"key: $movie, value: $rating")
    }





  }
  def main(args: Array[String]): Unit = {
    testMaps()

  }

}
